GenerativeComponents Help

Function definition by a User

In addition to the predefined functions, you can define new user-defined functions by using the FunctionCall node. The definition of the function is a GCScript definition written with the Script Editor. The Script Editor is used to define the function body.

For example, suppose you want to define a simple function called functionCall01 that takes an integer value and squares it, the expression could look like this:

double ScriptFunction01(double inputVal_1)
{
	double result = inputVal_1+inputVal_1;
	return result;
}

We define a variable name that takes on the value passed as an input to the function. This is referred to as the "argument" of the function. We need to declare its type. In this case we decided to define it as a double, and therefore it is defined as double input_Val_1. Because we want the function to calculate a value (the addition of our input), we need to define the return type of the function. This is done by adding the double type in front of the function keyword. Inside the function body we declare a new variable that contains the result of our calculations. To make this clear we choose the name to be result, but it could be any other name as well. It is a double, too. We assign the value of sum of input_Val_1 with itself to the result. In the next line, use the return keyword that indicates that the function takes on the value of the result variable, in this case the outcome of our calculation. When you assign the function in the FunctionCall, for instance, to define the XTranslation value of a point.

Notice how we use the name of our function, ScriptFunction01, called in the functionCall01 node. Because the function returns a value based on its input, it can be used instead of a number directly as it takes on the value from the return of the function. In this case we see the value it takes on shown as a value result of a function.



Script Function called in the functionCall returns the value 4 and 10, based on the inputVal_1 supplied 2, 5 respectively.

In this example, the input_Val_1 is doubled by the script function. The resultant value determines the X position of point01.

Functions can be much more complex and can also return node types such as points or create nodes themselves. More information is available in the Script Functions section.